<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>04v-on</title>
    <style>
        .father {
            padding: 20px;
            color: red;
            border: 1px solid red;
            font-size: 30px;
        }
        
        .son {
            padding: 20px;
            color: blue;
            border: 1px solid blue;
            font-size: 25px;
        }
    </style>
</head>

<body>
    <div id="app">
        <button v-on:click="num++">点赞</button>
        <!-- 事件指定一个回调函数，必须是vue实例中methods定义的函数，v-on:click可以简写为@click -->
        <button @click="cancel" v-if="num>0">取消赞</button> <br> 有{{num}}个赞
        <!-- 事件冒泡，点击子div会冒泡到父div，如何阻止，添加事件修饰符：@click.stop -->
        <!-- once，只执行一次 -->
        <div class="father" v-on:click.once="handle">
            父div
            <div class="son" @click.stop="handle">子div
                <br>
                <!-- prevent，阻止默认事件发生 -->
                <a href="http://www.baidu.com" @click.prevent.stop="handle">去百度</a>
            </div>
        </div>
        <!-- 按键修饰符 -->
        <!-- 按上下键可以控制num的数量，还可以组合键ctrl加点击修改num为5 -->
        <input type="text" v-model="num" v-on:keyup.up="num+=2" @keyup.down="num-=2" @click.ctrl="num=5">
        <br> 提示：
    </div>
    <script src="../node_modules/vue/dist/vue.js"></script>
    <script>
        let vm = new Vue({
            el: "#app",
            data: {
                num: 1,
            },
            methods: {
                cancel() {
                    this.num--
                },
                handle() {
                    alert("点击了")
                }
            },
        })
    </script>
</body>

</html>